<template>
	<view class="view_step2">
		<button class="search_ble_btn" @click="senBleLabelFor">打印</button>
	</view>
</template>

<script>
import { showConfirm } from '../../../../utils/common';
	export default {
		props:{
			btn:{
				type:Boolean,
				default:true,
			},
			data:{
				type:Object,
				default:()=>{
					return {}
				}
			}
		},
		emits:['close'],
		data() {
			return {
				blePlatform:uni.requireNativePlugin("TH-PlatformBLE"),
				templateType: "60*40",
			};
		},
		methods:{
			senBleLabelFor() {
				let that = this
				this.countPrint ++
				let data = {	
					company:"Indemax",
					goods_at:"2024-11-02", 
					goods_bar_code:"http://indemax.xnrcms.cn/prod/goodscode/barcode/337254753811.png",
					goods_code:"",
					goods_no: "337254753811",
					goods_qr_code:"http://indemax.xnrcms.cn/prod/goodscode/qrcode/337254753811.png",		            
					//model：数据
					code: "QR1234567890ABCDEF",
					materialName: '王鹏',
					materialCode: "123456789",
					supplierName: "容奥",
					mfgTime: "2023-06-06",
					qty: "1205",
					po: "4500014628",
					supplierBatch: "20230815-S001133",			
				}
				//标签模式
				console.log("打印1", data)
				let canvasWidth = 60;
				let canvasHeight = 45;
				let { toLine, splitStr, notNullData,blePlatform } = that;
				blePlatform.clear();//新增方法 清除数据
				blePlatform.setSize(60, 45); //标签纸规格
				blePlatform.setGap(0);
				blePlatform.setCls();
				blePlatform.setBackFeed(0);
				let col = 0;
				let font = "TSS16.BF2";
				blePlatform.setQR(toLine(3), toLine(1), "L", 8, "A", notNullData(data.goods_no));
				// let codeStr = notNullData(data.goods_no);
				// let codeList = splitStr(codeStr, 11);
				// for (let codeListIndex = 0; codeListIndex < codeList.length; codeListIndex++) {
				// 	blePlatform.setText(toLine(11), toLine((5 + codeListIndex)), font, 1, 1, codeList[codeListIndex]);
				// }
				// blePlatform.setText(col, toLine(2), font, 2, 2, notNullData(data.materialCode));
				// blePlatform.setText(col, toLine(4), font, 1, 1, "物料名称:")
				// let materialName = notNullData(data.company)
				// let materialNameList = splitStr(materialName, 20)
				let i = 0
				// for (; i < materialNameList.length; i++) {
				// 	blePlatform.setText(col, toLine(5 + i), font, 1, 1, materialNameList[i])
				// }
				blePlatform.setText(col, toLine(11 + i), font, 1, 1,  "供应商简称:" + notNullData(data.company+data.company+data.company+data.company))
				blePlatform.setText(col, toLine(12 + i), font, 1, 1,  "生产日期:" + notNullData(data.goods_at))
				blePlatform.setText(col, toLine(13 + i), font, 1, 1, "货号:" + notNullData(data.goods_no))
				console.log("打印2", blePlatform)
				blePlatform.setPagePrint()
				let ret = blePlatform.startPrint();//新增方法 开始打印
				console.log("打印2", "startPrint()"+ret)
				// if(this.countPrint <1){//打印10次
				// 	setTimeout(function() {
				// 		that.senBleLabelFor()
				// 	}, 500); // 这里的 1000 表示延时的时间，单位是毫秒
				// }
				// uni.canvasGetImageData({
				// 	canvasId: 'edit_area_canvas',
				// 	x: 0,
				// 	y: 0,
				// 	width: canvasWidth,
				// 	height: canvasHeight,
				// 	success: function(res) {
				// 		console.log("打印3", res)
				// 		// command.setBitmap(60, 0, 1, res)
				// 	},
				// 	complete: function() {
				// 		console.log("打印4", command)
				// 		command.setPagePrint()
				// 		that.prepareSend(command.getData())
				// 	}
				// })
			},
			// 标签 宽60 高80 横向
			labelTest60X80(data) {
				console.log("打印1", data)
				let that = this;
				let canvasWidth = that.canvasWidth
				let canvasHeight = that.canvasHeight
				let command =blePlatform
				let { toLine, splitStr, notNullData, scanInfo } = that
				command.setSize(60, 83)
				command.setGap(0)
				command.setCls()
				command.setBackFeed(0)
				let row = 20
				let font = "TSS24.BF2"
				command.setTextRotation(toLine(12), row, font, 90, 1, 1, "物料编码:");
				command.setTextRotation(toLine(11), row, font, 90, 2, 2, notNullData(data.materialCode))
				command.setTextRotation(toLine(9), row, font, 90, 1, 1, "物料名称:")
				let materialName = notNullData(scanInfo)
				let materialNameList = splitStr(materialName, 20)
				let i = 0
				for (; i < materialNameList.length; i++) {
					command.setTextRotation(toLine(8 - i), row, font, 90, 1, 1, materialNameList[i])
				}
				command.setTextRotation(toLine(8 - i), row, font, 90, 1, 1, "供应商简称:" + notNullData(data
					.supplierName))
				command.setTextRotation(toLine(7 - i), row, font, 90, 1, 1, "供应商生产白期:" + notNullData(data.mfgTime))
				command.setTextRotation(toLine(7 - i), toLine(10), font, 90, 1, 1,"数量:")
				command.setTextRotation(toLine(7 - i), toLine(12), font, 90, 2, 2, notNullData(data.qty))
				command.setTextRotation(toLine(6 - i), row, font, 90, 1, 1, "采购单号:" + notNullData(data.po))
				command.setTextRotation(toLine(5 - i), row, font, 90, 1, 1, "供应商生产批次:" + + notNullData(data
					.supplierBatch))
				command.setQRRotation(toLine(12), toLine(12), "L", 5, "A", 90, notNullData(data.code))
				let codeStr = notNullData(data.code)
				let codeList = splitStr(codeStr, 11)
				for (let codeListIndex = 0; codeListIndex < codeList.length; codeListIndex++) {
					command.setTextRotation(toLine((9 - codeListIndex)), toLine(12), font, 90, 1, 1, codeList[
						codeListIndex])
				}

				console.log("打印2", command)
				uni.canvasGetImageData({
					canvasId: 'edit_area_canvas',
					x: 0,
					y: 0,
					width: canvasWidth,
					height: canvasHeight,
					success: function(res) {
						console.log("打印3", res)
						// command.setBitmap(60, 0, 1, res)
					},
					complete: function() {
						console.log("打印4", command)
						command.setPagePrint()
					}
				})
			},
			// 获取非空数据
			notNullData(data) {
				return (data ? data : data == 0 ? 0 : "")
			},
			// 文本分割
			splitStr(str, len) {
				let reg = new RegExp(".{" + len + "}", 'g');
				let list = str.match(reg)
				if (list == null) {
					list = []
				}
				// 尾数添加
				list.push(str.substr(list.length * len, str.length % len))
				return list
			},
			toLine(index) {
				let that = this
				let lineHeight = 30
				if (that.templateType == '80*60') {
					lineHeight = 30
				} else if (that.templateType == '40*30') {
					lineHeight = 30 * 0.6
				} else if (that.templateType == '60*80') {
					lineHeight = 30 * 1.2
				} else if (that.templateType == '60*40') {
					lineHeight = 30 * 1
				}
				return lineHeight * index
			},
		}
	}
</script>

<style lang="scss">
.view_step2{
	.search_ble_btn{
		font-size: 30rpx;
		line-height: 80rpx;
	}
	.ble_list{
		margin-top: 40rpx;
		.ble_item{
			font-size: 38rpx;
			line-height: 80rpx;
			color: #0081FF;
			&+.ble_item{
				margin-top: 20rpx;
			}
			.icon_status{
				margin-left: 10rpx;
				color: #0081FF;
				&::before{
					color: #0081FF;
				}
			}
		}
	}
	.ble_loading{
		position: relative;
		width: 35px;
		height: 35px;
		perspective: 800px;
		transform-style: preserve-3d;
		transition: all 0.2s ease-out;
	}
}

</style>
